/*
 * @lc app=leetcode.cn id=36 lang=javascript
 *
 * [36] 有效的数独
 */

// @lc code=start
/**
 * @param {character[][]} board
 * @return {boolean}
 */
var isValidSudoku = function (board) {
  const set = new Set();

  // 行检测
  for (let i = 0; i < 9; i++) {
    for (let j = 0; j < 9; j++) {
      if (board[i][j] === ".") continue;
      if (set.has(board[i][j])) {
        return false;
      }
      set.add(board[i][j]);
    }
    set.clear();
  }

  // 列检测
  for (let i = 0; i < 9; i++) {
    for (let j = 0; j < 9; j++) {
      if (board[j][i] === ".") continue;
      if (set.has(board[j][i])) {
        return false;
      }
      set.add(board[j][i]);
    }
    set.clear();
  }

  // 宫格检测
  let x, y;
  for (let i = 0; i < 9; i += 3) {
    for (let j = 0; j < 9; j += 3) {
      for (let p = 0; p < 3; p++) {
        for (let q = 0; q < 3; q++) {
          x = i + p;
          y = j + q;
          if (board[x][y] === ".") continue;
          if (set.has(board[x][y])) {
            return false;
          }
          set.add(board[x][y]);
        }
      }
      set.clear();
    }
  }
  return true;
};
// @lc code=end
